package com.baijiayun.live.ui.toolbox.redpacket;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ListView;

import androidx.core.content.ContextCompat;

import com.baijiayun.live.ui.R;
import com.baijiayun.live.ui.base.BaseFragment;
import com.baijiayun.live.ui.toolbox.redpacket.widget.TransparentSurfaceView;
import com.baijiayun.livecore.models.LPRedPacketModel;
import com.baijiayun.livecore.models.RedPacketTopModel;
import com.baijiayun.livecore.utils.LPLogger;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;


/**
 * 鲸溪定制UI-红包雨效果
 *
 * @author panzq
 * @date 20190514
 */
public class RedPacketFragment extends BaseFragment implements RedPacketContract.View {
    private RedPacketContract.Presenter mPresenter;
    private TransparentSurfaceView mTsf;
    private ListView mLvJignxiTop;
    private RedPacketTopAdapter mTopAdapter;
    private int mCurrStateType;
    private ValueAnimator redPocketScaleInAnimator;
    private LPRedPacketModel redPacketModel;

    public static RedPacketFragment newInstance(LPRedPacketModel redPacketModel) {
        Bundle args = new Bundle();
        args.putSerializable("redPacketModel", redPacketModel);
        RedPacketFragment fragment = new RedPacketFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.bjy_fragment_red_packet;
    }

    @Override
    public void setPresenter(RedPacketContract.Presenter presenter) {
        this.mPresenter = presenter;
        setBasePresenter(presenter);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        if(getArguments() != null){
            redPacketModel = (LPRedPacketModel) getArguments().getSerializable("redPacketModel");
        }
        mTsf = (TransparentSurfaceView) $.id(R.id.tsf_red_packet).view();
        mLvJignxiTop = (ListView) $.id(R.id.lv_fragment_top).view();
        mTopAdapter = new RedPacketTopAdapter(getContext());
        mLvJignxiTop.setAdapter(mTopAdapter);

        $.id(R.id.rl_red_packet).view().setOnClickListener(v -> {
            //屏蔽点击传递其他View
        });

        $.id(R.id.btn_reb_phb_close).view().setOnClickListener(v -> {
            mPresenter.exit(false);
        });
        //没抢到
        $.id(R.id.tv_red_not_next).clicked(v -> {
            if(mTopAdapter.getCount() > 0){
                mPresenter.switchState(RedPacketContract.TYPE_REDPACKET_RANKING_LIST);
            }
        });
        //抢到了
        $.id(R.id.tv_red_have_button).clicked(v -> {
            if(mTopAdapter.getCount() > 0){
                mPresenter.switchState(RedPacketContract.TYPE_REDPACKET_RANKING_LIST);
            }
        });
        // region 红包结束结果 老师助教的页面
        $.id(R.id.bjy_red_packet_result_container).background(ThemeDataUtil.getCommonWindowBg(getContext()));
        $.id(R.id.base_window_title_name).text(getContext().getString(R.string.base_red_packet_title));
        $.id(R.id.base_window_title_close).clicked(v -> mPresenter.exit(false));
        $.id(R.id.bjy_red_packet_result_republish).clicked(v -> mPresenter.exit(true));
        $.id(R.id.bjy_red_packet_result_republish).background(ThemeDataUtil.getDrawableNegativeButtonBg(getContext()));
        $.id(R.id.bjy_red_packet_result_republish).textColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(
                getContext(), com.baijiayun.liveuibase.R.attr.base_theme_dialog_negative_text_color));

        $.id(R.id.bjy_red_packet_result_watch).clicked(v -> mPresenter.showResultList());
        $.id(R.id.bjy_red_packet_result_watch).background(ThemeDataUtil.getDrawableProductButtonBg(getContext()));
        $.id(R.id.bjy_red_packet_result_watch).textColor(ContextCompat.getColor(getContext(), com.baijiayun.liveuibase.R.color.base_white));
        // endregion

        switchRedPacketStart(RedPacketContract.TYPE_REDPACKET_START_COUNTDOWN);
    }

    @Override
    public void updateRedPacketTime(long timeStart) {
        $.id(R.id.tv_red_packet_time_start).text(String.valueOf(timeStart));
        if (timeStart == 0) {
            //显示go动画
            PropertyValuesHolder alphaProper = PropertyValuesHolder.ofFloat("alpha", 1f, 0f);
            PropertyValuesHolder scaleXProper = PropertyValuesHolder.ofFloat("scaleX", 1f, 2f);
            PropertyValuesHolder scaleYProper = PropertyValuesHolder.ofFloat("scaleY", 1f, 2f);
            ValueAnimator animator = ObjectAnimator.ofPropertyValuesHolder($.id(R.id.red_pocket_go_tv).view(), alphaProper, scaleXProper, scaleYProper);
            animator.setDuration(166);
            animator.start();
            $.id(R.id.red_pocket_go_tv).visible();
        }
    }

    @Override
    public void switchRedPacketRankingList(RedPacketTopModel[] list) {
        switchRedPacketStart(RedPacketContract.TYPE_REDPACKET_RANKING_LIST);
        if (list == null || list.length == 0) {
            //显示表情
            $.id(R.id.rl_lp_ui_redpack_none).visibility(View.VISIBLE);
            $.id(R.id.lv_fragment_top).visibility(View.INVISIBLE);
        } else {
            $.id(R.id.rl_lp_ui_redpack_none).visibility(View.GONE);
            $.id(R.id.lv_fragment_top).visibility(View.VISIBLE);
        }
        mTopAdapter.setData(list);
    }

    @Override
    public void switchRedPacketStart(int type) {
        switch (type) {
            case RedPacketContract.TYPE_REDPACKET_START_COUNTDOWN:
                if (redPocketScaleInAnimator != null) {
                    redPocketScaleInAnimator.removeAllListeners();
                }
                PropertyValuesHolder alphaProper = PropertyValuesHolder.ofFloat("alpha", 0.5f, 1f);
                PropertyValuesHolder scaleXProper = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 1f);
                PropertyValuesHolder scaleYProper = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 1f);
                redPocketScaleInAnimator = ObjectAnimator.ofPropertyValuesHolder($.id(R.id.rl_fragment_redpacket_time).view(), alphaProper, scaleXProper, scaleYProper);
                redPocketScaleInAnimator.setDuration(166);
                redPocketScaleInAnimator.start();
                redPocketScaleInAnimator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mPresenter.startRedPacket();
                    }
                });

                $.id(R.id.rl_red_packet).visibility(View.VISIBLE);
                showContainer(R.id.rl_fragment_redpacket_time);
                break;
            case RedPacketContract.TYPE_REDPACKET_RUNNING:
                showContainer(R.id.tsf_red_packet);

                mTsf.setOnClickRedPacketListener(model -> {
                    //点击请求
                    mPresenter.robRedPacket(model);
                });
                mTsf.start(redPacketModel);
                break;
            case RedPacketContract.TYPE_REDPACKET_RANKING_LIST:
                mTsf.pause();
                showContainer(R.id.rl_fragment_redpacket_phb);
                break;
            case RedPacketContract.TYPE_REDPACKET_EXIT:
                mPresenter.exit(false);
                break;
            case RedPacketContract.TYE_REDPACKET_NOT_ROB: {
                if (mCurrStateType == RedPacketContract.TYPE_REDPACKET_RANKING_LIST) {
                    return;
                }
                mTsf.pause();
                //一个没抢到
                ScaleAnimation animation = new ScaleAnimation(0.4f, 1f, 0.4f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                animation.setDuration(800);
                $.id(R.id.rl_not_red).view().setAnimation(animation);
                animation.startNow();
                showContainer(R.id.rl_not_red);
                break;
            }
            case RedPacketContract.TYPE_REDPACKET_ROB: {
                if (mCurrStateType == RedPacketContract.TYPE_REDPACKET_RANKING_LIST) {
                    return;
                }
                LPLogger.d("RedPacketPresenter", "显示我抢红包的数量 " + mPresenter.getScoreAmount());
                mTsf.pause();
                //显示抢到的积分
                ScaleAnimation animation = new ScaleAnimation(0.4f, 1f, 0.4f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                animation.setDuration(800);
                $.id(R.id.rl_red_rob).view().setAnimation(animation);
                animation.startNow();
                showContainer(R.id.rl_red_rob);
                $.id(R.id.tv_red_rob_credit).text(String.valueOf(mPresenter.getScoreAmount()));
                break;
            }
            case RedPacketContract.TYPE_REDPACKET_ROB_FINISH:
                mTsf.pause();
                showContainer(R.id.bjy_red_packet_result_container);
                $.id(R.id.bjy_red_packet_result_republish).visibility(mPresenter.isTeacher() ? View.VISIBLE : View.GONE);
                break;
            default:break;
        }
        mCurrStateType = type;
    }

    /**
     * 展示 指定 container 的Id，隐藏其他的 container
     * @param id 要展示的 container id
     */
    private void showContainer(int id) {
        $.id(R.id.bjy_red_packet_result_container).visibility(id == R.id.bjy_red_packet_result_container ? View.VISIBLE : View.GONE);
        $.id(R.id.rl_red_rob).visibility(id == R.id.rl_red_rob ? View.VISIBLE : View.GONE);
        $.id(R.id.rl_not_red).visibility(id == R.id.rl_not_red ? View.VISIBLE : View.GONE);
        $.id(R.id.rl_fragment_redpacket_phb).visibility(id == R.id.rl_fragment_redpacket_phb ? View.VISIBLE : View.GONE);
        $.id(R.id.tsf_red_packet).visibility(id == R.id.tsf_red_packet ? View.VISIBLE : View.GONE);
        $.id(R.id.rl_fragment_redpacket_time).visibility(id == R.id.rl_fragment_redpacket_time ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setRobEnable(boolean robEnable) {
        if (mTsf == null) {
            return;
        }
        mTsf.setRobEnable(robEnable);
    }

    @Override
    public void onDestroy() {
        if (mPresenter != null) {
            mPresenter.release();
        }
        super.onDestroy();
        if (mTsf != null) {
            mTsf.pause();
            mTsf.destroy();
            mTsf = null;
        }
        if (redPocketScaleInAnimator != null) {
            redPocketScaleInAnimator.removeAllListeners();
        }
    }
}
